﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMS
{
    public class UserRelate
    {
        #region 1.0 根据用户Id，获取用户姓名
        /// <summary>
        /// 根据用户Id，获取用户姓名
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string GerUserNameById(int Id)
        {
            string UserName = null;
            var user = DALSession.Current.BD_UserInfoDAL.GetListBy(u => u.ID == Id).FirstOrDefault();
            if (user != null)
            {
                UserName = user.UserName;
            }
            return UserName;
        }
        #endregion


        #region 2.0 获取所有的人员列表
        /// <summary>
        /// 根据用户Id，获取用户姓名
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<View_BD_UserDepInfo> GetAllUsers()
        {
            return DALSession.Current.View_BD_UserDepInfoDAL.GetListBy(u => u.UserState == "0").ToList();
        }
        #endregion

    

        #region 3.0 创建Combo递归树二级节点集合
        /// <summary>
        /// 创建Combo递归树二级节点集合
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<TreeNode> GetComboUserTree(List<BD_DepInfo> allDep, List<View_BD_UserDepInfo> allUsers, List<int> checkUser)
        {
            List<TreeNode> listNodes = new List<TreeNode>();
            foreach (var dep in allDep)
            {
                TreeNode node = new TreeNode() { id = dep.ID, text = dep.DepName, state = "closed", Checked = true };
                listNodes.Add(node);
                List<View_BD_UserDepInfo> thisDepUser = allUsers.Where(u => u.DepId == dep.ID).ToList();
                LoadTreeNodeByDepId(node, thisDepUser, checkUser);
            }
            return listNodes;
        }
        #endregion



        public static void LoadTreeNodeByDepId(TreeNode Node, List<View_BD_UserDepInfo> thisDepUser, List<int> checkUser)
        {
            List<TreeNode> listNodes = new List<TreeNode>();
            foreach (var s in thisDepUser)
            {
                if (checkUser.Contains(s.ID))
                {
                    listNodes.Add(new TreeNode() { id = s.ID, text = s.UserName, Checked = true });
                }
                else
                {
                    listNodes.Add(new TreeNode() { id = s.ID, text = s.UserName, Checked = false });
                }
            }
            if (listNodes.Count == 0)
            {
                Node.children = null;
            }
            else
            {
                Node.children = listNodes;
            }


        }
        #region 4.0根据id创建Combo选人
        public static List<ComboList> GetComboById(int Id)
        {

            List<int> userIds = DALSession.Current.BD_ComboUserDAL.GetListBy(c => c.ComboId == Id).Select(c => c.UserId).ToList();

            return DALSession.Current.BD_UserInfoDAL.GetListBy(u => userIds.Contains(u.ID)).Select(c => new ComboList() { value = c.ID.ToString(), text = c.UserName }).ToList();
        }
        #endregion
    }
}